package com.ctgu.sell.controller;

import com.ctgu.sell.VO.*;
import com.ctgu.sell.dataobject.ProductCategory;
import com.ctgu.sell.dataobject.ProductInfo;
import com.ctgu.sell.service.CategoryService;
import com.ctgu.sell.service.ProductService;
import com.ctgu.sell.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 买家商品
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO list() {

        //1. 查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2. 查询类目(一次性查询)
        List<Integer> categoryTypeList = new ArrayList<>();
        //传统方法
        for (ProductInfo productInfo : productInfoList) {
            categoryTypeList.add(productInfo.getCategoryType());
        }
        //精简方法(java8, lambda)
//        List<Integer> categoryTypeList = productInfoList.stream()
//                .map(e -> e.getCategoryType())
//                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //3. 数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory: productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo: productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
//        ResultVO resultVO = new ResultVO();
//        resultVO.setData(productVOList);
//        resultVO.setCode(0);
//        resultVO.setMsg("成功");
//
//        return resultVO;
        return ResultVOUtil.success(productVOList);
    }
    @GetMapping("/listcategory")
    public ResultVO listcategory(){
        //1. 查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2. 查询类目(一次性查询)
        List<Integer> categoryTypeList = new ArrayList<>();
        //传统方法
        for (ProductInfo productInfo : productInfoList) {
            if(!categoryTypeList.contains(productInfo.getCategoryType()))
                categoryTypeList.add(productInfo.getCategoryType());
        }
        List<Productleimu> productleimuList = new ArrayList<>();
        for (Integer i: categoryTypeList) {
            Productleimu product1 = new Productleimu();
            product1.setCategoryType(categoryService.findOne(i).getCategoryType());
            product1.setCategoryName(categoryService.findOne(i).getCategoryName());

            productleimuList.add(product1);
        }
        ResultVO resultVO = new ResultVO();
        resultVO.setData(productleimuList);
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        return resultVO;
    }


    @GetMapping("/caidan")
    public ResultVO listCaidan(){
        //1. 查询所有的上架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        List<Productcaidan> productlcaidanlist = new ArrayList<>();

        for (ProductInfo productInfo : productInfoList) {
            Productcaidan productcaidan = new Productcaidan();
            productcaidan.setProductId(productInfo.getProductId());
            productcaidan.setProductName(productInfo.getProductName());
            productcaidan.setProductThumb(productInfo.getProductThumb());
            productcaidan.setProductIcon(productInfo.getProductIcon());
            productcaidan.setProductPrice(productInfo.getProductPrice());
            productcaidan.setProductUnit(productInfo.getProductUnit());
            productcaidan.setProductType(productInfo.getCategoryType());
            productcaidan.setProductSales(productInfo.getProductSales());
            productcaidan.setProductDescription(productInfo.getProductDescription());
            productcaidan.setProductNumb1(productInfo.getProductNumb1());

            productlcaidanlist.add(productcaidan);
        }

        ResultVO resultVO = new ResultVO();
        resultVO.setData(productlcaidanlist);
        resultVO.setCode(0);
        resultVO.setMsg("成功");
        return resultVO;

    }
}
